/**
 * @author Dawn on 2016-02-25.
 */
var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1));
    var sURLVariables = sPageURL.split('&');
    var sParameterName = undefined;

    for (var i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

var apiUrl = "http://localhost:8080";

var AjaxIO = {
    _getUrl: function (action) {
        return apiUrl + action + ".do";
    },

    getJSON: function (url, param, callback) {
        url = this._getUrl(url);
        if (callback != undefined) {
            $.getJSON(url, param, callback);
        } else {
            callback = param;
            $.getJSON(url, callback);
        }
    },

    post: function (url, param, callback) {
        $.post(this._getUrl(url), param).done(callback);
    }
};

var StringUtils = {
    isEmpty: function (str) {
        if (!str) {
            //str is empty
            return true;
        }
        return false;

    },

    isNotEmpty: function (str) {
        return !this.isEmpty(str);
    }
};

var GetCookie = function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

var SetCookie = function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}


/**
 * 分页
 */
function PageKit(params, pageEvent) {
    this.divId = params.id;
    this.totalPage = params.totalPage;
    this.currentPage = 1;
    this.pageSkipEvent = pageEvent;
    var _self = this;
    var pageDiv = $('#' + _self.divId);
    pageDiv.html("");
    pageDiv.append("<a class='page-up' href='javascript:void(0)' >上一页</a>");
    pageDiv.append("<span class='first-num'>1/" + params.totalPage + "</span>");
    pageDiv.append("<a class='page-down' href='javascript:void(0)'>下一页</a>");
    pageDiv.append("<input type='text' class='inputPage'>");
    pageDiv.append("<button class='btn btn-primary'>跳转</button>");

    pageDiv.on('click', 'button', function () {
        var inputValue = _self._inputValue();
        if (isNaN(inputValue)) {
            return;
        }

        _self.skipToPage(parseInt(inputValue));
    });

    //上一页
    pageDiv.on('click', '.page-up', function () {
        _self.skipToPage(_self.currentPage - 1);
    });

    //下一页
    pageDiv.on('click', '.page-down', function () {
        _self.skipToPage(_self.currentPage + 1);
    });
}

PageKit.prototype._inputValue = function () {
    return $('#' + this.divId + ' input').val()
};

PageKit.prototype.skipToPage = function (pageNum) {
    if (isNaN(pageNum) || pageNum < 1 || pageNum > this.totalPage) {
        return;
    }

    $('#' + this.divId + ' .first-num').html(pageNum + "/" + this.totalPage);
    this.currentPage = pageNum;
    $('#' + this.divId + ' input').val('');
    this.pageSkipEvent(pageNum);
};

var Page = {

    render: function (params, pageEvent) {
        return new PageKit(params, pageEvent);
    }
};
